from rest_framework import serializers
from django.contrib.auth import get_user_model
from .models import AgentType, Agent, AgentRating, Conversation, Message

User = get_user_model()


class AgentTypeSerializer(serializers.ModelSerializer):
    """智能体类型序列化器"""
    class Meta:
        model = AgentType
        fields = '__all__'


class AgentSerializer(serializers.ModelSerializer):
    """智能体序列化器"""
    creator_name = serializers.CharField(source='creator.username', read_only=True)
    agent_type_name = serializers.CharField(source='agent_type.name', read_only=True)
    
    class Meta:
        model = Agent
        fields = '__all__'
        read_only_fields = ('creator', 'usage_count', 'rating', 'rating_count', 'created_at', 'updated_at', 'last_used_at')
    
    def create(self, validated_data):
        validated_data['creator'] = self.context['request'].user
        return super().create(validated_data)


class AgentCreateSerializer(serializers.ModelSerializer):
    """智能体创建序列化器"""
    class Meta:
        model = Agent
        fields = ('name', 'description', 'avatar', 'agent_type', 'system_prompt', 
                 'personality', 'capabilities', 'knowledge_domains', 'model_name', 
                 'temperature', 'max_tokens', 'is_public')
    
    def create(self, validated_data):
        validated_data['creator'] = self.context['request'].user
        return super().create(validated_data)


class AgentUpdateSerializer(serializers.ModelSerializer):
    """智能体更新序列化器"""
    class Meta:
        model = Agent
        fields = ('name', 'description', 'avatar', 'system_prompt', 'personality', 
                 'capabilities', 'knowledge_domains', 'model_name', 'temperature', 
                 'max_tokens', 'is_public', 'status')


class AgentRatingSerializer(serializers.ModelSerializer):
    """智能体评分序列化器"""
    user_name = serializers.CharField(source='user.username', read_only=True)
    
    class Meta:
        model = AgentRating
        fields = '__all__'
        read_only_fields = ('user', 'created_at')
    
    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class MessageSerializer(serializers.ModelSerializer):
    """消息序列化器"""
    class Meta:
        model = Message
        fields = '__all__'
        read_only_fields = ('created_at',)


class ConversationSerializer(serializers.ModelSerializer):
    """对话序列化器"""
    agent_name = serializers.CharField(source='agent.name', read_only=True)
    user_name = serializers.CharField(source='user.username', read_only=True)
    messages = MessageSerializer(many=True, read_only=True)
    message_count = serializers.SerializerMethodField()
    
    class Meta:
        model = Conversation
        fields = '__all__'
        read_only_fields = ('user', 'created_at', 'updated_at')
    
    def get_message_count(self, obj):
        return obj.messages.count()
    
    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class ConversationCreateSerializer(serializers.ModelSerializer):
    """对话创建序列化器"""
    class Meta:
        model = Conversation
        fields = ('agent', 'title')
    
    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class ChatMessageSerializer(serializers.Serializer):
    """聊天消息序列化器"""
    message = serializers.CharField(max_length=5000)
    conversation_id = serializers.IntegerField(required=False)